// 初始化websocket
var websocket;
var audio = document.getElementById('v');

var video = {
	pause : function (time){
		audio.currentTime = time;
		audio.pause();
	},
	play : function(time){
		if (audio !== null) {
			if (audio.currentTime == 0) {
				audio.play();
			} else {
				audio.currentTime = time;
				audio.play();
			}
		}
	},
	go_to : function(time){
		audio.currentTime = time;
		audio.play();
	}
}


$(function(){
	//用户的全局唯一标识：事件id+个人id，中间以逗号隔开
	var user_guid = "2";
	if ('WebSocket' in window) {
		websocket = new WebSocket(
				"ws://127.0.0.1:8080/yl-websocket/ws?uid=" + user_guid);
	} else if ('MozWebSocket' in window) {
		websocket = new MozWebSocket(
				"ws://127.0.0.1:8080/yl-websocket/ws?uid=" + user_guid);
	} else {
		websocket = new SockJS(
				"http://127.0.0.1:8080/yl-websocket/ws/sockjs?uid=" + user_guid);
	}
	websocket.onmessage = function(event) {
		var data = JSON.parse(event.data);
		if (data.text.indexOf("bf") > -1) {
			var time = data.text.split("bf")[1];
			video.play(time);
		} else if (data.text.indexOf("zt") > -1) {
			var time = data.text.split("zt")[1];
			video.pause(time);
		} else if (data.text.indexOf("dwbf") > -1) {
			var time = data.text.split("dwbf")[1];
			video.go_to(time);
		}
		
		if (data.from > 0 || data.from == -1) {
			
		} else if (data.from == 0) {
			
		} else if (data.from == -2) {
			
		}
	};
	
	$.post("onlineusers", function(data) {
		for (var i = 0; i < data.length; i++)
			$("#users").append(
					'<a href="#" onclick="talk(this)" class="list-group-item">'
							+ data[i] + '</a>');
	});

	$("#broadcast").click(function() {
		$.post("broadcast", {
			"text" : $("#msg").val()
		});
	});

	$("#send")
			.click(
					function() {
						var v = $("#myinfo").val();
						if (v == "") {
							return;
						} else {
							var data = {};
							data["from"] = uid;
							data["fromName"] = uid;
							data["to"] = to_uid;
							data["text"] = v;
							websocket
									.send(JSON.stringify(data));
							$("#log-container")
									.append(
											"<div class='bg-success'><label class='text-info'>鎴�&nbsp;"
													+ new Date()
													+ "</label><div class='text-info'>"
													+ v
													+ "</div></div><br>");
							scrollToBottom();
							$("#myinfo").val("");
						}
					});

});
